| Conditions | 1 |
| Paths | 1 |
| Total Lines | 85 |
| Lines | 85 |
| Ratio | 100 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | |||
| 4 | describe('16-bit to bytes', function() { |
||
| 5 | |||
| 6 | let byteData = require('../../index.js'); |
||
| 7 | |||
| 8 | // 16-bit / 2 bytes |
||
| 9 | // signed |
||
| 10 | it('should turn 2 signed 16-bit ints to 4 bytes (0s)', function() { |
||
| 11 | assert.deepEqual(byteData.toBytes([0, 0], 16, {"base": 10}), |
||
| 12 | [0, 0, 0, 0]); |
||
| 13 | }); |
||
| 14 | it('should turn 1 signed 16-bit int to 2 bytes (0)', function() { |
||
| 15 | assert.deepEqual(byteData.toBytes([0], 16, {"base": 10}), |
||
| 16 | [0, 0]); |
||
| 17 | }); |
||
| 18 | it('should turn 2 signed 16-bit ints to 4 bytes (max range)', function() { |
||
| 19 | assert.deepEqual(byteData.toBytes([-32768, 32767], 16, {"base": 10}), |
||
| 20 | [0, 128, 255, 127] |
||
| 21 | ); |
||
| 22 | }); |
||
| 23 | it('should turn 1 16-bit signed int to 1 byte hex (-1)', function() { |
||
| 24 | assert.deepEqual(byteData.toBytes([-1], 16, {"base": 16}), |
||
| 25 | ['ff', 'ff']); |
||
| 26 | }); |
||
| 27 | |||
| 28 | // unsigned |
||
| 29 | it('should turn 2 unsigned 16-bit ints to 4 bytes (0s)', function() { |
||
| 30 | assert.deepEqual(byteData.toBytes([0, 0], 16, {"base": 10}), |
||
| 31 | [0, 0, 0, 0]); |
||
| 32 | }); |
||
| 33 | it('should turn 1 unsigned 16-bit int to 2 bytes (0)', function() { |
||
| 34 | assert.deepEqual(byteData.toBytes([0], 16, {"base": 10}), |
||
| 35 | [0, 0]); |
||
| 36 | }); |
||
| 37 | it('should turn 2 unsigned 16-bit ints to 4 bytes (max range)', function() { |
||
| 38 | assert.deepEqual(byteData.toBytes([0, 65535], 16, {"base": 10}), |
||
| 39 | [0, 0, 255, 255] |
||
| 40 | ); |
||
| 41 | }); |
||
| 42 | it('should turn 1 unsigned 16-bit int to 2 bytes (0)', function() { |
||
| 43 | assert.deepEqual(byteData.toBytes([765], 16, {"base": 16}), |
||
| 44 | ["fd", "02"]); |
||
| 45 | }); |
||
| 46 | |||
| 47 | // 16-bit floats: 0s |
||
| 48 | it('should turn 2 bytes to 1 16-bit float (0)', function() { |
||
| 49 | assert.deepEqual(byteData.toBytes( |
||
| 50 | [0], 16, {"base": 10, "float": true}), |
||
| 51 | [0, 0]); |
||
| 52 | }); |
||
| 53 | it('should turn 2 bytes hex to 1 16-bit float (0)', function() { |
||
| 54 | assert.deepEqual(byteData.toBytes( |
||
| 55 | [0], 16, {"base": 10, "float": true}), |
||
| 56 | ["00", "00"]); |
||
| 57 | }); |
||
| 58 | it('should turn 2 bytes bin to 1 16-bit float (0)', function() { |
||
| 59 | assert.deepEqual(byteData.toBytes( |
||
| 60 | [0], 16, {"base": 2, "float": true}), |
||
| 61 | ["00000000", "00000000"]); |
||
| 62 | }); |
||
| 63 | it('should turn 2 bytes hex to 1 16-bit float (1)', function() { |
||
| 64 | assert.deepEqual(byteData.toBytes( |
||
| 65 | [1], 16, {"base": 16, "float": true}), |
||
| 66 | ["3c", "00"]); |
||
| 67 | }); |
||
| 68 | it('should turn 2 bytes hex to 1 16-bit float (1/3)', function() { |
||
| 69 | assert.deepEqual(byteData.toBytes( |
||
| 70 | [0.33325], 16, {"base": 16, "float": true}), |
||
| 71 | ["35", "55"]); |
||
| 72 | }); |
||
| 73 | it('should turn 2 bytes hex to 1 16-bit float (-2)', function() { |
||
| 74 | assert.deepEqual(byteData.toBytes( |
||
| 75 | [-2], 16, {"base": 16, "float": true}), |
||
| 76 | ["c0", "00"]); |
||
| 77 | }); |
||
| 78 | it('should turn 2 bytes hex to 1 16-bit float (65504)', function() { |
||
| 79 | assert.deepEqual(byteData.toBytes( |
||
| 80 | [65504], 16, {"base": 16, "float": true}), |
||
| 81 | ["7b", "ff"]); |
||
| 82 | }); |
||
| 83 | it('should turn 4 bytes hex to 2 16-bit float (65504, 0.33325, extra byte)', function() { |
||
| 84 | assert.deepEqual(byteData.toBytes( |
||
| 85 | [65504, 0.33325], 16, {"base": 16, "float": true}), |
||
| 86 | ["7b", "ff", "35", "55"]); |
||
| 87 | }); |
||
| 88 | }); |
||
| 89 |